//
//  ReadViewController.m
//  news
//
//  Created by imacbookair on 16/3/9.
//  Copyright © 2016年 imacbookair. All rights reserved.
//

#import "ReadViewController.h"
#import "PrefixHeader.pch"
@interface ReadViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *dataArr;
@property (nonatomic, strong)NSString *str;
@end

@implementation ReadViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor grayColor];
    self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    /** 按钮 **/
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"搜索" style:UIBarButtonItemStylePlain target:self action:@selector(shousuo)];
    
    
    /** 数据处理 **/
    [self headerData];
    [self dataFooter];
    /** 重用池 **/
    [_tableView registerClass:[ReadTableViewCell class] forCellReuseIdentifier:@"cell"];
    /** 刷新加载方法 **/
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self headerData];
            [self.tableView.mj_header endRefreshing];
        });
    }];
    self.tableView.mj_header = header;
    
    MJRefreshAutoFooter *footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        [self dataFooter];
        [self.tableView.mj_footer endRefreshing];
    }];
    self.tableView.mj_footer = footer;
    
}

- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}
#pragma mark - 数据

- (void)headerData{
    _str = @"http://api.liwushuo.com/v2/channels/101/items?limit=20&ad=2&gender=1&offset=0&generation=2";
    [NewWorking GetDayaWithURL:_str dic:nil sussess:^(id responseObject) {
        self.dataArr = [ReadModel modelHanderWithArray:responseObject[@"data"][@"items"]];
        
        [self.tableView reloadData];
    } filed:^(NSError *error) {
        
    }];

}

- (void)dataFooter{
    
    static NSInteger t = 20;
  _str = @"http://api.liwushuo.com/v2/channels/101/items?limit=20&ad=2&gender=1&offset=0&generation=2";
    NSString *urlStr = [_str stringByReplacingOccurrencesOfString:@"offset=0" withString:[NSString stringWithFormat:@"offset=%ld", (long)t]];
    [NewWorking GetDayaWithURL:urlStr dic:nil sussess:^(id responseObject) {
        NSArray *arrTemp = [NSArray array];
        arrTemp = [ReadModel modelHanderWithArray:responseObject[@"data"][@"items"]];
        for (NSInteger i = 0; i < arrTemp.count; i++) {
            [self.dataArr addObject:arrTemp[i]];
        }
        [_tableView reloadData];
        
    } filed:^(NSError *error) {
        
    }];
    
    
    t += 10;

}
#pragma mark - tableView 设置
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ReadTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    ReadModel *m = _dataArr[indexPath.row];
    [cell.tupian sd_setImageWithURL:[NSURL URLWithString:m.cover_image_url]];
    [cell NightWithType:UIViewColorTypeNormal];
    cell.title.text = m.title;
    
    
    return cell;
    
}
#pragma mark - 设置cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 200;
}

#pragma mark - 设置cell的点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ReadModel *m = _dataArr[indexPath.row];
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    ReadXqViewController *readx = [[ReadXqViewController alloc]init];
    readx.name = m.title;
    readx.docid = m.url;
    readx.shoucangModel = m;
    
    [self.navigationController pushViewController:readx animated:YES];

}
#pragma mark - 搜素按钮
- (void)shousuo{
    SearchViewController *Search = [[SearchViewController alloc]init];
    Search.str =_str;
    [self.navigationController pushViewController:Search animated:YES];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
